from wsgiref import headers

import requests
from bs4 import BeautifulSoup
from urllib3.util import url

class pcScrawler():
    def __init__(self):
        self.headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.1.3162 SLBChan/103'
        }

    def getCodes(self, url):
            return requests.get(url, headers=self.headers).text

    def getTexts(self, url, selector):
        bs = BeautifulSoup(self.getCodes(url), 'html.parser')
        return [item.text for item in bs.select(selector=selector)]

    def getUrl(self, url, selector):
        bs = BeautifulSoup(self.getCodes(url), 'html.parser')
        return [item.attrs['href'] for item in bs.select(selector=selector)]

    def getImgUrl(self, url, selector):
        bs = BeautifulSoup(self.getCodes(url), 'html.parser')
        return [item.attrs['src'] for item in bs.select(selector=selector)]

    def getImg(self, url, imgName=None):
        rs = requests.get(url,headers=self.headers).content
        with open(imgName,'wb') as f:
            f.write(rs)